package com.liao.controller;

import com.liao.bean.Count;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/count")
public class CountController {

    @Autowired
    private CountService countService;

    @ResponseBody
    @RequestMapping("/queryCountListByPage")
    public PageResult<Count> queryCountListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(defaultValue = "") String orderId) throws Exception {
        PageResult<Count> pageResult = countService.queryCountListByPage(currentPage,pageSize,orderId);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("/addCount")
    public Result addCount(@RequestBody Count count) throws Exception {

        countService.addCount(count);
        return new Result(true, Code.MEMBER_ADD_SUCCESS, Msg.MEMBER_ADD_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("/editCount")
    public Result editCount( @RequestBody Count count) throws Exception {
        countService.editCount(count);
        return new Result(true,Code.MEMBER_EDIT_SUCCESS, Msg.MEMBER_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteCount")
    public Result deleteCount(String ocid) throws Exception {
        countService.deleteCount(ocid);
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteCountIds")
    public Result deleteCountIds(@RequestParam("ocids[]") List<String> ocids) throws Exception {
        for (String ocid : ocids) {
            try {
                countService.deleteCount(ocid);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.MEMBER_DELETE_FAIL, Msg.MEMBER_DELETE_FAIL);
            }

        }
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }
}
